#include "mainwindows.h"
#include "ui_mainwindows.h"
#include "qimage.h"
#include <QPaintEvent>
#include <QPainter>
#include <QHttpRequestHeader>
#include "resourcemanager.h"

MainWindows::MainWindows(QWidget *parent)
    : QDialog(parent), ui(new Ui::MainWindowsClass)
{
    ui->setupUi(this);
    ui->progressBar->setVisible(false);
    m_r = 0;
    connect(&m_onemanga, SIGNAL(previousReady(QString)), this, SLOT(onPrevReady(QString)));
    connect(&m_onemanga, SIGNAL(nextReady(QString)), this, SLOT(onNextReady(QString)));
    connect(&m_onemanga, SIGNAL(imageReady(QByteArray)), this, SLOT(onImgReady(QByteArray)));
    connect(&m_onemanga, SIGNAL(mangasReady(QMap<QString, QString>)), this, SLOT(onMangasReady(QMap<QString,QString>)));

    updatecboBookmark();

    m_onemanga.getMangas();
}

MainWindows::~MainWindows()
{
    delete ui;
    delete m_r;
    disconnect(&m_onemanga, SIGNAL(previousReady(QString)), this, SLOT(onPrevReady(QString)));
    disconnect(&m_onemanga, SIGNAL(nextReady(QString)), this, SLOT(onNextReady(QString)));
    disconnect(&m_onemanga, SIGNAL(imageReady(QByteArray)), this, SLOT(onImgReady(QByteArray)));
    disconnect(&m_onemanga, SIGNAL(mangasReady(QMap<QString, QString>)), this, SLOT(onMangasReady(QMap<QString,QString>)));
}

 void MainWindows::paintEvent(QPaintEvent * event)
 {
    ui->lbl1->setGeometry(ui->lbl1->geometry().x(),ui->lbl1->geometry().y(),this->geometry().width()-40,this->geometry().height()-70);
 }

void MainWindows::on_btnView_clicked()
{
    int     index       = ui->cboBookmark->currentIndex();
    QString imagePath   = ui->cboBookmark->itemData(index).toString();
    m_bookmarkaddress   = imagePath;

    // If a manga was selected, the image path is built using the chapter and page instead of
    // the current bookmark.
    if (ui->cboMangas->currentIndex() != 0)
    {
        index           = ui->cboMangas->currentIndex();
        QString address = ui->cboMangas->itemData(index).toString();
        QString chapter = ui->cboChapters->currentText();
        QString page    = ui->cboPages->currentText();
        imagePath       = QString("/%1/%2/%3/").arg(address).arg(chapter).arg(page);
        m_bookmarkaddress   = "";
    }

    m_onemanga.getImage(imagePath);
    ui->progressBar->setVisible(true);
    ui->btnSuiv->setEnabled(false);
    ui->btnPrec->setEnabled(false);
    connect(&m_onemanga, SIGNAL(progressStep(int)), ui->progressBar, SLOT(setValue(int)));
}

void MainWindows::onPrevReady(QString s)
{
    m_prec = s;
    ui->btnPrec->setEnabled(true);
}
void MainWindows::onNextReady(QString s)
{
    m_suiv = s;
    ui->btnSuiv->setEnabled(true);
}
void MainWindows::onImgReady(QByteArray b)
{
    ui->lbl1->setImage(b);
    ui->progressBar->setVisible(false);
    ui->progressBar->reset();
    disconnect(&m_onemanga, SIGNAL(progressStep(int)), ui->progressBar, SLOT(setValue(int)));
}
void MainWindows::on_btnPrec_clicked()
{
    m_onemanga.getImage(m_prec);
    ui->progressBar->setVisible(true);
    connect(&m_onemanga, SIGNAL(progressStep(int)), ui->progressBar, SLOT(setValue(int)));
}

void MainWindows::on_btnSuiv_clicked()
{
    m_onemanga.getImage(m_suiv);
    ui->progressBar->setVisible(true);
    connect(&m_onemanga, SIGNAL(progressStep(int)), ui->progressBar, SLOT(setValue(int)));
}

void MainWindows::onMangasReady(QMap<QString, QString> mangas)
{
    ui->cboMangas->clear();
    ui->cboMangas->addItem("");

    // Add every mangas found along with their address.
    // The selected manga and address can be retrieved using:
    // int      index           = ui->cboMangas->currentIndex();
    // QString  mangaName       = ui->cboMangas->itemText(index);
    // QString  mangaAddress    = ui->cboMangas->itemData(index).toString();
    for (int i = 0; i < mangas.count(); i++)
    {
	QString s = mangas.values()[i];
	ui->cboMangas->addItem(mangas.keys()[i], mangas.values()[i] );
    }
}

void MainWindows::on_cboMangas_currentIndexChanged(int index)
{
    if(index != 0)
	ui->cboBookmark->setCurrentIndex(0);
}

void MainWindows::on_cboBookmark_currentIndexChanged(int index)
{
    if(index != 0)
    {
	ui->cboMangas->setCurrentIndex(0);
        ui->cboChapters->setCurrentIndex(0);
        ui->cboPages->setCurrentIndex(0);
    }
}

void MainWindows::on_btnMajBookmark_clicked()
{
    if(m_bookmarkaddress.compare("") != 0)
    {
	BookmarkList * bml = (BookmarkList*)ResourceManager::getBookmarkInstance(ResourceManager::Xml)->getResources();
	for(int i=0;i<bml->count();i++)
	{
	    Bookmark* b = (Bookmark*)bml->at(i);
	    if(m_bookmarkaddress.compare("/" + b->manga()->adresse() + b->chapitre() + "/" + b->page() + "/") == 0)
	    {
		b->setChapitre(m_onemanga.getCurrentAddress().split("/")[2]);
		b->setPage(m_onemanga.getCurrentAddress().split("/")[3]);
	    }
	}
    }
    else
    {
	ResourceManager::getBookmarkInstance(ResourceManager::Xml)->addResource(new Bookmark(0,ui->cboMangas->currentText(),
		ui->cboMangas->itemData(ui->cboMangas->currentIndex()).toString()+"/",m_onemanga.getCurrentAddress().split("/")[2],
		m_onemanga.getCurrentAddress().split("/")[3],123,false));
    }
    updatecboBookmark();
}

void MainWindows::on_btnBookmark_clicked()
{
    m_frm.setResourceList( ResourceManager::getBookmarkInstance(ResourceManager::Xml));
    m_frm.setModal(true);
    m_frm.show();
    connect(&m_frm,SIGNAL(finished(int)), this, SLOT(onFrmMangaFinished(int)));
}
void MainWindows::updatecboBookmark()
{
    BookmarkList* bml = (BookmarkList*)ResourceManager::getBookmarkInstance(ResourceManager::Xml)->getResources();
    ui->cboBookmark->clear();
    ui->cboBookmark->addItem("   ");
    for(int i=0;i<bml->count();i++)
    {
	Bookmark * b = (Bookmark*)bml->at(i);
	ui->cboBookmark->addItem(b->manga()->nom() + " Chap:" + b->chapitre() + " Page:" + b->page(),"/" + b->manga()->adresse() + b->chapitre() + "/" + b->page() + "/");
    }
}
void MainWindows::onFrmMangaFinished(int result)
{
    updatecboBookmark();
    disconnect(&m_frm,SIGNAL(finished(int)), this, SLOT(onFrmMangaFinished(int)));
}
